package cn.edu.lsnu.mbti.quartz;

import cn.edu.lsnu.mbti.entity.Page;
import cn.edu.lsnu.mbti.entity.University;
import cn.edu.lsnu.mbti.service.MajorService;
import cn.edu.lsnu.mbti.service.PageService;
import cn.edu.lsnu.mbti.service.UniversityService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.*;
import java.util.concurrent.TimeUnit;

import static cn.edu.lsnu.mbti.common.RedisConstants.HOT_UNIVERSITY_EXPIRE;
import static cn.edu.lsnu.mbti.common.RedisConstants.HOT_UNIVERSITY_KEY;

public class MyTask extends QuartzJobBean {

    private RedisTemplate redisTemplate;
    private PageService pageService;
    private UniversityService universityService;
    private MajorService majorService;

    public MyTask(RedisTemplate redisTemplate, PageService pageService, UniversityService universityService, MajorService majorService) {
        this.redisTemplate = redisTemplate;
        this.pageService = pageService;
        this.universityService = universityService;
        this.majorService = majorService;
    }




    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        //更新热点院校
        //1.获取时间key
        long hour = System.currentTimeMillis() / (1000 * 60 * 60);
        String key = HOT_UNIVERSITY_KEY + String.valueOf(hour);

        //2.获取热度最高的page （院校）
        LambdaQueryWrapper<Page> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Page::getAccessTotal);
        List<Page> pageList = pageService.list(wrapper);
        List<Page> finalPageList = new ArrayList<>();
        if (pageList != null && pageList.size() > 0) {
            for (int i = 0; i < pageList.size(); i++) {
                if (i == 5) {
                    break;
                }
                String id = pageList.get(i).getRouteId();
                University university = universityService.getById(id);
                //判断是否是院校
                if (ObjectUtil.isNotNull(university)) {
                    finalPageList.add(pageList.get(i));
                }
            }
        }

        //3.从redis中删除数据
        redisTemplate.delete(key);

        //向redis中添加新数据
        Map<String,Long> saveData = new HashMap<>();
        for (Page page : finalPageList) {
            University university = universityService.getById(page.getRouteId());
            if (ObjectUtil.isNotNull(university)) {
                saveData.put(university.getName(), page.getAccessTotal());
            }
        }
        Set<String> keySet = saveData.keySet();
        for (String name : keySet) {
            redisTemplate.opsForZSet().add(key, name, saveData.get(name));
            redisTemplate.expire(key,HOT_UNIVERSITY_EXPIRE, TimeUnit.SECONDS);
        }

    }
}
